package com.rocksdata.mc.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ctz.commonframe.constant.DeviceInfo;
import com.ctz.commonframe.ui.view.EmbeddedGridView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.Logger;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.CampusAppManager;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.location.UnreadManager;
import com.rocksdata.mc.entity.CampusAppInfo;
import com.rocksdata.mc.R;
import com.rocksdata.mc.entity.UnreadCache;
import com.rocksdata.mc.ui.activity.AppManagerActivity2;
import java.util.ArrayList;
import java.util.List;

import q.rorbin.badgeview.QBadgeView;

/**
 * Created by 陈堂正 on 2017/5/23.
 */

public class AppWidgetView extends BaseWidgetView {
    private List<CampusAppInfo> campusAppList;
    private AppGridAdapter appAdapter;

    public AppWidgetView(Context context) {
        super(context);
    }

    public AppWidgetView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setupHeadView() {
        layoutHead.setVisibility(View.GONE);
    }

    @Override
    public View getContentView() {
//        UnreadManager.setUnread("app44", "1");
//        UnreadManager.setUnread("app44", "2");
//        UnreadManager.setUnread("app44", "3");
//        UnreadManager.setUnread("app44", "4");
        EmbeddedGridView gvApp = new EmbeddedGridView(mContext);
//        gvApp.setLayoutParams(new LinearLayout.LayoutParams(
//                LinearLayout.LayoutParams.MATCH_PARENT, DeviceInfo.screenWidth / 2 + 20));
//        gvApp.setLayoutParams(new LinearLayout.LayoutParams(
//                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        gvApp.setCacheColorHint(Color.TRANSPARENT);
        gvApp.setVerticalScrollBarEnabled(false);
        gvApp.setHorizontalScrollBarEnabled(false);
        gvApp.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
        gvApp.setNumColumns(4);
        gvApp.setSelector(new ColorDrawable(Color.TRANSPARENT));
        campusAppList = new ArrayList<>();
        appAdapter = new AppGridAdapter(mContext, campusAppList);
        gvApp.setAdapter(appAdapter);
        gvApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == campusAppList.size()) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, AppManagerActivity2.class);
                    mContext.startActivity(intent);
                }else if(position < campusAppList.size()){
                    CampusAppInfo info = (CampusAppInfo) parent.getItemAtPosition(position);
                    CampusAppManager.skipToChildApplication(mContext, info);
                }
            }
        });
        return gvApp;
    }

    @Override
    public void bindData() {
        Logger.e("AppWidgetView bindData");
//        if (appAdapter != null)
//            appAdapter.notifyDataSetChanged();
        BusinessManager.getApplicationManager().syncAppList(new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                List<CampusAppInfo> apps = (List<CampusAppInfo>) object;
                campusAppList.clear();
                campusAppList.addAll(apps);
                appAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void release() {

    }

    private class AppGridAdapter extends AbsAdapter<CampusAppInfo> {

        private AppGridAdapter(Context context, List<CampusAppInfo> datas) {
            super(context, datas, R.layout.item_campus_app_grid);
        }

        @Override
        public int getCount() {
            int count = datas.size() + 1;
            if(count % 4 == 0){
                return count;
            }else{
                return 4 * (count / 4 + 1);
            }
        }

        @Override
        public void convert(AdapterViewHolder holder, CampusAppInfo data) {
            ImageView unreadFlag = holder.getView(R.id.iv_app_control);
            ImageView icon = holder.getView(R.id.iv_app_icon);
            TextView name = holder.getView(R.id.tv_app_name);
            QBadgeView badgeView;
            if (data != null) {
                if (unreadFlag.getTag() == null) {
                    badgeView = new QBadgeView(context);
                    badgeView.bindTarget(unreadFlag);
                    badgeView.setBadgeGravity(Gravity.CENTER | Gravity.CENTER);
                    badgeView.setBadgeTextSize(10, true);
                    badgeView.setBadgePadding(2, true);
                    unreadFlag.setTag(badgeView);
                } else {
                    badgeView = (QBadgeView) unreadFlag.getTag();
                }

                UnreadCache cache = UnreadManager.getUnreadCache();
                if (cache != null) {
                    int unreadCount = cache.getUnreadCount(data.getCode());
                    if (unreadCount > 0) {
                        badgeView.setBadgeNumber(unreadCount);
                    } else {
                        badgeView.hide(false);
                    }
                } else {
                    badgeView.hide(false);
                }
            }

            if(holder.getPosition() == datas.size()){
                icon.setImageResource(R.mipmap.ic_app_all);
                name.setText("所有应用");
            }else if(holder.getPosition() < datas.size()){
                if(data != null) {
                    GlideApp.with(icon.getContext()).load(data.getIcon()).into(icon);
                    name.setText(data.getName());
                }
            }else{
                icon.setImageDrawable(null);
                name.setText("");
            }
            holder.getConvertView().setLayoutParams(new AbsListView.LayoutParams(
                    AbsListView.LayoutParams.MATCH_PARENT, DeviceInfo.screenWidth / 4));
        }
    }
}
